package com.lalilu.homework8.fragment;

import android.content.ContentValues;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;
import android.widget.Toast;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.fragment.app.Fragment;

import com.lalilu.homework8.R;
import com.lalilu.homework8.utils.DBHelper;

public class SqliteFragment extends Fragment {

    @Override
    public void onViewCreated(@NonNull View view, @Nullable Bundle savedInstanceState) {
        super.onViewCreated(view, savedInstanceState);
        DBHelper dbHelper = DBHelper.getInstance(requireContext());

        EditText _username = view.findViewById(R.id.sqlite_username);
        EditText _age = view.findViewById(R.id.sqlite_age);
        Button _insert = view.findViewById(R.id.sqlite_insert);
        Button _search = view.findViewById(R.id.sqlite_search);
        TextView _output = view.findViewById(R.id.sqlite_output);

        _output.post(() -> {
            getFromDatabase(dbHelper, _output, null, null);
        });

        _search.setOnClickListener(v -> {
            getFromDatabase(dbHelper, _output, "age<18", null);
        });

        _insert.setOnClickListener(v -> {
            SQLiteDatabase db = dbHelper.getWritableDatabase();
            ContentValues contentValues = new ContentValues();
            contentValues.put("username", _username.getText().toString());
            contentValues.put("age", _age.getText().toString());

            long ref = db.insert("users", null, contentValues);
            String msg = ref == -1 ? "添加失败" : "添加成功";
            Toast.makeText(getActivity(), msg, Toast.LENGTH_SHORT).show();
            db.close();
        });
    }

    private void getFromDatabase(DBHelper dbHelper, TextView textView, String selection, String[] selectionArgs) {
        SQLiteDatabase db = dbHelper.getReadableDatabase();
        StringBuilder stringBuilder = new StringBuilder();
        Cursor cursor = db.query("users", null, selection,
                selectionArgs, null, null, null);
        while (cursor.moveToNext()) {
            long id = cursor.getLong(cursor.getColumnIndex("id"));
            String username = cursor.getString(cursor.getColumnIndex("username"));
            long age = cursor.getLong(cursor.getColumnIndex("age"));
            stringBuilder.append("id: ").append(id)
                    .append("    username: ").append(username)
                    .append("    age: ").append(age)
                    .append("\n");
        }
        textView.setText(stringBuilder.toString());
        cursor.close();
        db.close();
    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        // Inflate the layout for this fragment
        return inflater.inflate(R.layout.fragment_sqlite, container, false);
    }
}